package com.abvnet.project.water.api.web;

import com.abvnet.common.utils.StringUtils;
import com.abvnet.common.utils.file.FileUploadUtils;
import com.abvnet.framework.config.AbvnetConfig;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.project.water.project.domain.ProjectCustomerLog;
import com.abvnet.project.water.project.service.IProjectCustomerLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api/customer/log")
public class ApiProjectCustomerLogController extends BaseController {

    @Autowired
    private IProjectCustomerLogService projectCustomerLogService;

    @GetMapping("/list")
    public AjaxResult list(ProjectCustomerLog projectCustomerLog) {
        return AjaxResult.success(projectCustomerLogService.selectProjectCustomerLogListForApi(projectCustomerLog));
    }

    @GetMapping("/get")
    public AjaxResult get(@RequestParam("customerLogId") Long customerLogId) {
        return AjaxResult.success(projectCustomerLogService.selectProjectCustomerLogById(customerLogId));
    }

    @PostMapping("/add")
    public AjaxResult addSave(ProjectCustomerLog projectCustomerLog, @RequestParam(name = "scene1File",
            required = false) MultipartFile scene1File,
                              @RequestParam(name = "scene2File", required = false) MultipartFile scene2File,
                              @RequestParam(name = "scene3File", required = false) MultipartFile scene3File) throws IOException {
        if (scene1File != null && !scene1File.isEmpty()) {
            String sceneUrl = FileUploadUtils.upload(AbvnetConfig.getCustomerLogPath(), scene1File);
            projectCustomerLog.setSceneUrl1(sceneUrl);
        }
        if (scene2File != null && !scene2File.isEmpty()) {
            String sceneUrl = FileUploadUtils.upload(AbvnetConfig.getCustomerLogPath(), scene2File);
            projectCustomerLog.setSceneUrl2(sceneUrl);
        }
        if (scene3File != null && !scene3File.isEmpty()) {
            String sceneUrl = FileUploadUtils.upload(AbvnetConfig.getCustomerLogPath(), scene3File);
            projectCustomerLog.setSceneUrl3(sceneUrl);
        }
        return toAjax(projectCustomerLogService.insertProjectCustomerLog(projectCustomerLog));
    }

    @PostMapping("/edit")
    public AjaxResult editSave(ProjectCustomerLog projectCustomerLog, @RequestParam(name = "scene1File",
            required = false) MultipartFile scene1File,
                               @RequestParam(name = "scene2File", required = false) MultipartFile scene2File,
                               @RequestParam(name = "scene3File", required = false) MultipartFile scene3File) throws IOException {
        if(StringUtils.isNotEmpty(projectCustomerLog.getSceneDelete1())) {
            if(StringUtils.equals("1",projectCustomerLog.getSceneDelete1())) {
                projectCustomerLog.setSceneUrl1(StringUtils.EMPTY);
            }
        }
        if (scene1File != null && !scene1File.isEmpty()) {
            String sceneUrl = FileUploadUtils.upload(AbvnetConfig.getCustomerLogPath(), scene1File);
            projectCustomerLog.setSceneUrl1(sceneUrl);
        }
        if(StringUtils.isNotEmpty(projectCustomerLog.getSceneDelete2())) {
            if(StringUtils.equals("1",projectCustomerLog.getSceneDelete2())) {
                projectCustomerLog.setSceneUrl2(StringUtils.EMPTY);
            }
        }
        if (scene2File != null && !scene2File.isEmpty()) {
            String sceneUrl = FileUploadUtils.upload(AbvnetConfig.getCustomerLogPath(), scene2File);
            projectCustomerLog.setSceneUrl2(sceneUrl);
        }
        if(StringUtils.isNotEmpty(projectCustomerLog.getSceneDelete3())) {
            if(StringUtils.equals("1",projectCustomerLog.getSceneDelete3())) {
                projectCustomerLog.setSceneUrl3(StringUtils.EMPTY);
            }
        }
        if (scene3File != null && !scene3File.isEmpty()) {
            String sceneUrl = FileUploadUtils.upload(AbvnetConfig.getCustomerLogPath(), scene3File);
            projectCustomerLog.setSceneUrl3(sceneUrl);
        }
        return toAjax(projectCustomerLogService.updateProjectCustomerLog(projectCustomerLog));
    }
}
